/**
 * 
 */
package com.juyu.miliao.common.util.oss;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.servlet.http.Part;

import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;




/**
 * @author pengs
 *
 */
public class OSSUtil {
	
	public static String putObject(OSSConfig ossConfig, MultipartFile file, String key) {
		try {
			return putObject(ossConfig, file.getInputStream(), file.getSize(), key, file.getOriginalFilename());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static String putObject(OSSConfig ossConfig, Path file, String key) {
		try {
			return putObject(ossConfig, Files.newInputStream(file), Files.size(file), key, file.getFileName().toString());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static String putObject(OSSConfig ossConfig, Part file, String key) {
		try {
			return putObject(ossConfig, file.getInputStream(), file.getSize(), key, file.getSubmittedFileName());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static String putObject(OSSConfig ossConfig, InputStream input, long size, String key, String fileName) {
		OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(),
				ossConfig.getAccessKeySecret());
		try {
			ObjectMetadata meta = new ObjectMetadata();
			meta.setContentLength(size);
			//meta.setContentType("application/vnd.android.package-archive");
			//meta.setContentDisposition("attachment;filename=" + fileName);
			ossClient.putObject(ossConfig.getBucketName(), key, input, meta);
			return key;
		} finally {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			ossClient.shutdown();
		}
	}
	/*获取*/
	public static OSSObject getObject(OSSConfig ossConfig, String key){
		OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
		try{
			return ossClient.getObject(ossConfig.getBucketName(), key);
		} finally {
			ossClient.shutdown();
		}
	}
	/*复制*/
	public static String copyObject(OSSConfig ossConfig, String sourceKey, String destinationKey){
		OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
		try{
			ossClient.copyObject(ossConfig.getBucketName(), sourceKey, ossConfig.getBucketName(), destinationKey);
			return destinationKey;
		} finally {
			ossClient.shutdown();
		}
	}
	/*设置权限*/
	public static void setObjectAcl(OSSConfig ossConfig, String key, CannedAccessControlList cannedACL){
		OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
		try{
			ossClient.setObjectAcl(ossConfig.getBucketName(), key, CannedAccessControlList.Private);
		} finally {
			ossClient.shutdown();
		}
	}
	
	/*删除文件*/
	public static void deleteObject(OSSConfig ossConfig, String key){
		OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
		try{
			ossClient.deleteObject(ossConfig.getBucketName(), key);
		} finally {
			ossClient.shutdown();
		}
	}
}
